﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrawlerCore;
using WCrawlerLib;
using System.Threading;
using System.Threading.Tasks;

namespace WCrawler.Controls
{
    public partial class ucHostFile : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        
        }

        protected void btnAddFiles_Click(object sender, EventArgs e)
        {
            btnAdd.Visible = true;
            txtHmtlCode.Rows = 10;
            lblMessage.Text = "";
            txtHmtlCode.Text = "";
            mdlPopup.Show();
        }

        protected void btnCheckLinks_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> finalChecklinks = new Dictionary<string,string>();
            if (Session["PostId"] != null)
            {
                FilePost objFilePost = new FilePost();
                int postId = (int)Session["PostId"];
                finalChecklinks = objFilePost.CheckFilePostsByPost(postId);
                Session["CheckLinks"] = finalChecklinks;
                gridViewFilePosts.DataBind();
            }
            else
            {
                object objLock = new object();
                Post objPost = new Post();
                IQueryable<Post> query = objPost.GetAll();
                Parallel.ForEach(query, p =>
                {
                    FilePost objFilePost = new FilePost();
                    Dictionary<string, string> checklinks = objFilePost.CheckFilePostsByPost(p.PostId);
                    lock (objLock)
                    {
                        foreach (KeyValuePair<string, string> value in checklinks)
                        {
                            if (!finalChecklinks.Keys.Contains(value.Key))
                                finalChecklinks.Add(value.Key, value.Value);
                        }
                    }
                });
                Session["CheckLinks"] = finalChecklinks;
                gridViewFilePosts.DataBind();
            }
        }

        protected void gridViewFilePosts_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblFilesUrl = (Label)e.Row.FindControl("lblFilesUrl");
                if (lblFilesUrl != null)
                {
                    Dictionary<string, string> checklinks = null;
                    if (Session["CheckLinks"] != null)
                    {
                        checklinks = (Dictionary<string, string>)Session["CheckLinks"];
                    }
                    FilePost objFilePost = new FilePost();
                    List<string> fileList = objFilePost.RowDataBoundCheckLinks(lblFilesUrl.Text, checklinks);
                    lblFilesUrl.Text = string.Join("<br />", fileList.ToArray());
                }
            }
        }

        public event EventHandler FilePostLinqDataSourceSelecting;
        protected void FilePostLinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (FilePostLinqDataSourceSelecting != null)
                FilePostLinqDataSourceSelecting(sender, e);
            FilePost objFilePost = new FilePost();
            if (Session["PostId"] != null)
            {
                int postId = (int)Session["PostId"];
                e.Result = objFilePost.GetFilePostsByPost(postId);
            }
        }

        public void GridViewFilePosts_DataBind()
        {
            gridViewFilePosts.DataBind();
        }

        public event EventHandler AddFile;
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (AddFile != null)
                AddFile(sender, e);
            string files = txtHmtlCode.Text;
            int postId = (int)Session["PostId"];
            string[] fileArr = files.Split(new char[] { '\n' });
            string hostName = GeneralClass.GetName(fileArr[0]);
            Host objHost = new Host();
            objHost = objHost.GetHost(hostName);
            if (objHost == null)
            {
                lblMessage.Text = "File Host not found";
            }
            else
            {
                int hostId = objHost.HostId;
                //Add download files
                FilePost objFilePost = new FilePost();
                objFilePost.AddFilePosts(postId, hostId, files);
                //Update download files in postdetail

                txtHmtlCode.Text = "";
                gridViewFilePosts.DataBind();
                lblMessage.Text = "Add Successful";
            }
        }
    }
}